package org.jeecg.modules.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.travel.code.CheckStatusCode;
import org.jeecg.modules.travel.dto.noticeBar.*;
import org.jeecg.modules.travel.entity.JhNoticeBar;
import org.jeecg.modules.travel.service.IJhNoticeBarService;
import org.jeecg.modules.travel.vo.noticeBar.AppNoticeBarRecordVo;
import org.jeecg.modules.travel.vo.noticeBar.PcNoticeBarDetailVo;
import org.jeecg.modules.travel.vo.noticeBar.PcNoticeBarRecordVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@Tag(name="通知公告表")
@RestController
@RequestMapping("/travel/jhNoticeBar")
@Slf4j
public class JhNoticeBarController extends JeecgController<JhNoticeBar, IJhNoticeBarService> {

    @Autowired
    private IJhNoticeBarService jhNoticeBarService;

    @Operation(summary="通知公告-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<PcNoticeBarRecordVo>> queryPageList(PcNoticeBarPageDto dto, HttpServletRequest req) {
        JhNoticeBar noticeBar = new JhNoticeBar();
        BeanUtils.copyProperties(dto, noticeBar);
        QueryWrapper<JhNoticeBar> queryWrapper = QueryGenerator.initQueryWrapper(noticeBar, req.getParameterMap());
        queryWrapper.lambda().eq(JhNoticeBar::getDelFlag, CommonConstant.DEL_FLAG_0);
        Page<JhNoticeBar> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        return Result.OK(jhNoticeBarService.queryPage(page, queryWrapper));
    }

    /**
     *   添加
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "通知公告-添加")
    @Operation(summary="通知公告-添加")
    @RequiresPermissions("org:jh_notice_bar:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody @Valid NoticeBarAddDto dto) {
        String res = jhNoticeBarService.noticeBarAdd(dto);
        if (StringUtils.isNotBlank(res)) {
            return Result.error(res);
        }
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "通知公告-编辑")
    @Operation(summary="通知公告-编辑")
    @RequiresPermissions("org:jh_notice_bar:edit")
    @PostMapping(value = "/edit")
    public Result<String> edit(@RequestBody NoticeBarEditDto dto) {
        String res = jhNoticeBarService.noticeBarEdit(dto);
        if (StringUtils.isNotBlank(res)) {
            return Result.error(res);
        }
        return Result.OK("编辑成功!");
    }

    /**
     *  审核
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "通知公告-审核")
    @Operation(summary="通知公告-审核")
    @RequiresPermissions("org:jh_notice_bar:auth")
    @PostMapping(value = "/auth")
    public Result<String> auth(@RequestBody @Valid NoticeBarAuthDto dto) {
        String res = jhNoticeBarService.noticeBarAuth(dto,getUserId());
        if (StringUtils.isNotBlank(res)) {
            return Result.error(res);
        }
        return Result.OK("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通知公告-通过id删除")
    @Operation(summary="通知公告-通过id删除")
    @RequiresPermissions("org:jh_notice_bar:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        boolean flag = jhNoticeBarService.removeById(id);
        if (!flag) {
            return Result.error("删除失败!");
        }
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "通知公告-批量删除")
    @Operation(summary="通知公告-批量删除")
    @RequiresPermissions("org:jh_notice_bar:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        boolean flag = jhNoticeBarService.removeByIds(Arrays.asList(ids.split(",")));
        if (!flag) {
            return Result.error("批量删除失败!");
        }
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Operation(summary="通知公告-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<PcNoticeBarDetailVo> queryById(@RequestParam(name="id",required=true) String id) {
        JhNoticeBar noticeBar = jhNoticeBarService.getById(id);
        if (noticeBar == null) {
            return Result.error("幻灯片记录不存在");
        }
        PcNoticeBarDetailVo detailVo = new PcNoticeBarDetailVo();
        BeanUtils.copyProperties(noticeBar, detailVo);
        return Result.OK(detailVo);
    }

    /* ======================================= 移动端 ======================================================= */

    @Operation(summary="通知公告-列表查询")
    @GetMapping(value = "/app/appQueryList")
    public Result<IPage<AppNoticeBarRecordVo>> appQueryList(AppNoticeBarPageDto dto, HttpServletRequest req) {
        JhNoticeBar noticeBar = new JhNoticeBar();
        QueryWrapper<JhNoticeBar> queryWrapper = QueryGenerator.initQueryWrapper(noticeBar, req.getParameterMap());
        queryWrapper.lambda().eq(JhNoticeBar::getDelFlag, CommonConstant.DEL_FLAG_0)
                .eq(JhNoticeBar::getCheckStatus, CheckStatusCode.APPROVED)
                .orderByAsc(JhNoticeBar::getSortNo);
        Page<JhNoticeBar> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        return Result.OK(jhNoticeBarService.appQueryList(page,queryWrapper));
    }

}
